angular.module("lamallApp")
//插入页脚
.directive('shopDirective',function(){
	return{
		templateUrl:"views/footer.html",
		restrict:"ECMA",
		replace: false	
	}
})
.controller("shopCarController",function($scope,shopCarService,$timeout){
	shopCarService.getItems(function(res){
		console.log(res);
		$scope.data=res.data;
		// 给所有数据绑定值
	$scope.All=function(styleClass,bol){
		for(var i=0 ;i<$scope.data.length ;i++){
			for(var j=0; j<$scope.data[i].list[0].items.length ;j++){
					$scope.data[i].list[0].items[j].style=styleClass;
					$scope.data[i].list[0].items[j].styleStatus=bol;
			}
		}
	}	
	$scope.All("shopCar_Blabel",true);
//点击编辑
		$scope.Editor="编辑";
		$scope.bol=true;
//点击编辑时选框的状态
		$scope.styleCondition=function(){
			var a=0;
			var sum=0;
			for(var i=0 ;i<$scope.data.length ;i++){
					for(var j=0; j<$scope.data[i].list[0].items.length ;j++){
						sum++;					
						if($scope.bol==false){
							$scope.data[i].list[0].items[j].style="shopCar_Wlabel";
						}
						if($scope.bol==true && $scope.data[i].list[0].items[j].styleStatus==true){
							$scope.data[i].list[0].items[j].style="shopCar_Blabel";
							a++;
						}
						if($scope.bol==true && $scope.data[i].list[0].items[j].styleStatus==false){
							$scope.data[i].list[0].items[j].style="shopCar_Dlabel";
							// 如果有一个未选中，全选取消
							$scope.checkBol=false;
							$scope.checkAll="shopCar_Dlabel";
						}
						
					};
			}
			// 如果所有商品都被选中，激活全选框
			if(sum==a){
				$scope.checkAll="shopCar_Blabel";
				$scope.checkBol=true;
			}			
		}
		
		// 点击编辑或者完成
		$scope.EditorChange=function(){
			$scope.bol=! $scope.bol;
			$scope.bol==false ? $scope.Editor="完成" : $scope.Editor="编辑";
			$scope.styleCondition()
			console.log($scope.bol)
		}
		//单选框
		$scope.pitch=function(content){
			console.log(content)
				for(var i=0 ;i<$scope.data.length ;i++){
					for(var j=0; j<$scope.data[i].list[0].items.length ;j++){
						if(content==$scope.data[i].list[0].items[j]){
						// 如果在编辑状态
						if($scope.bol==false){
							$scope.data[i].list[0].items.splice(j,1);
							console.log($scope.data[i].list[0].items.length)					
							if($scope.data[i].list[0].items.length=="0"){
								$scope.data.splice(i,1);
							}
								$scope.count();	
							}
						//如果不在编辑状态
						if($scope.bol==true){
							$scope.data[i].list[0].items[j].styleStatus=!$scope.data[i].list[0].items[j].styleStatus;
							$scope.styleCondition();
							$scope.count();
						};	
					};
				}
			}
		}
		// 全选框
		$scope.checkAll="shopCar_Blabel";
		$scope.checkBol=true;
		$scope.check=function(){
			$scope.checkBol=!$scope.checkBol;
			if($scope.bol==true){
				$scope.checkBol==true ? $scope.checkAll="shopCar_Blabel" : $scope.checkAll="shopCar_Dlabel";
				$scope.checkBol==true ? $scope.All("shopCar_Blabel",true) : $scope.All("shopCar_Dlabel",false);
				$scope.count();
			}
			if($scope.bol==false){
				$scope.checkBol==true ? $scope.checkAll="shopCar_Blabel" : $scope.checkAll="shopCar_Dlabel";
				$scope.checkBol==true ? $scope.All("shopCar_Wlabel",true) : $scope.All("shopCar_Wlabel",false);
				$scope.count();
			}
		}
		//计算总价
		$scope.count=function(){
			$scope.total=0;
			for(var i=0 ;i<$scope.data.length ;i++){
				var items=$scope.data[i].list[0].items
				for(var j=0; j<items.length ;j++){
					if(items[j].styleStatus==true){
						var price=items[j].shop_price;
						var num=items[j].goods_number;
						$scope.total+=price*num;
					}
			
				};
			}
			$scope.total=($scope.total).toFixed(2);
		}
		$scope.count();
		//改变表框数量
		$scope.myFunc=function(myValue){
			console.log(myValue)
			this.content.goods_number=myValue;
			$scope.count();
		}


	})
})
